//
//  CLBaseSettingController.m
//  togetherLBS
//
//  Created by louis on 15/9/14.
//  Copyright (c) 2015年 celi. All rights reserved.
//

#import "CLBaseSettingController.h"
#import "CLSettingArrowItem.h"
#import "CLSettingIconItem.h"

@interface CLBaseSettingController ()<CLSettingCellDelegate>

@end

@implementation CLBaseSettingController
- (id)init
{
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
        self.tableView.showsVerticalScrollIndicator = NO;
        self.tableView.contentInset = UIEdgeInsetsMake(10, 0, 20, 0);
    }
    return self;
}

- (id)initWithStyle:(UITableViewStyle)style
{
    if (self = [super initWithStyle:UITableViewStyleGrouped]) {
        self.tableView.showsVerticalScrollIndicator = NO;
    }
    return self;
}

- (NSMutableArray *)groups
{
    if (_groups == nil) {
        _groups = [NSMutableArray array];
    }
    return _groups;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    CLSettingGroup *group = self.groups[section];
    return group.items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 创建cell
    CLSettingCell *cell = [CLSettingCell cellWithTableView:tableView];
    
    // 传递数据
    CLSettingGroup *group = self.groups[indexPath.section];
    cell.item = group.items[indexPath.row];
    cell.delegate = self;
    
    if ([self respondsToSelector:@selector(fontWithDetailTextLabel)]) {
        cell.detailTextLabel.font = [self fontWithDetailTextLabel];
    }
    
    // 返回cell
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CLSettingGroup *group = self.groups[indexPath.section];
    CLSettingItem *item = group.items[indexPath.row];
    if ([item isKindOfClass:[CLSettingIconItem class]]) {
        return 80;
    } else {
        return 44;
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    CLSettingGroup *group = self.groups[section];
    return group.header;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section
{
    CLSettingGroup *group = self.groups[section];
    return group.footer;
}

#pragma mark - 自定义section的headerView
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CLSettingGroup *group = self.groups[section];
    return group.headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    CLSettingGroup *group = self.groups[section];
    
    if (group.headerView) return group.headerView.frame.size.height;
    
    if (group.header) return 17.5;
    
    return 1.0;

}

#pragma mark - 自定义section的footerView
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    CLSettingGroup *group = self.groups[section];
    return group.footerView;
}

- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section
{
    UITableViewHeaderFooterView *footer = (UITableViewHeaderFooterView *)view;
//    if (!iOS7) {
        [footer setValue:@(NSTextAlignmentLeft) forKey:@"textAlignment"];
//    }
    // ios7 通过UITableViewHeaderFooterView的textLabel进行设置
    // ios6 通过上述方法设置无效，只能设置UITableViewHeaderFooterView的textAlignment属性（私有属性，必须通过kvc设置），但是该属性在ios7之后被取消掉了
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 1.取消选中这行
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    // 2.模型数据
    CLSettingGroup *group = self.groups[indexPath.section];
    CLSettingItem *item = group.items[indexPath.row];
    
    if (item.option) { // block有值(点击这个cell,.有特定的操作需要执行)
        item.option();
    } else if ([item isKindOfClass:[CLSettingArrowItem class]]) { // 箭头
        CLSettingArrowItem *arrowItem = (CLSettingArrowItem *)item;
        
        // 如果没有需要跳转的控制器
        if (arrowItem.destVcClass == nil) return;
        
        UIViewController *vc = [[arrowItem.destVcClass alloc] init];
        [self.navigationController pushViewController:vc  animated:YES];
    }
}


#pragma mark 拨动开关
- (void) settingCell:(CLSettingCell *)cell didChangeSwitchState:(BOOL)state{

    NSLog(@"settingCell.text %@",cell.textLabel.text);
    NSLog(@"state == %d",state);
}
@end
